<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \interop\Container\ContainerInterface;
use \Slim\Views\PhpRenderer;
use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;

class ExecutiveController 
{
	protected $app;
   	
   	public function __construct(ContainerInterface $ci) {
       $this->app = $ci;
   	}
   	public function __invoke($request, $response, $args) {
        //to access items in the container... $this->ci->get('');
   	}
   	
   	public function index($request, $response, $args){
    	global $flag,$msg,$data,$db;
  		$path = $this->app->router->pathFor('executiveIndex');

      //$push = wscpush('sengmsg','系统',0,'satff',0,'satff','msg','考勤系统已在新系统上线','通知');//群发推送
  		$as = [
  			'settings'=>$this->app->get('settings'),
  			'path' => $path
  		];
  		return $this->app->renderer->render($response, './executive.php', $as);
    }

    public function notice($request, $response, $args){
      global $flag,$msg,$data,$db;
      $path = $this->app->router->pathFor('executiveIndex');
    }

    public function noticedetail($request, $response, $args){
      global $flag,$msg,$data,$db;
      $path = $this->app->router->pathFor('executiveIndex');
      
    }

    public function savenotice($request, $response, $args){
      global $flag,$msg,$data,$db;
      $path = $this->app->router->pathFor('executiveIndex');
    }

    public function worktime($request, $response, $args){
      global $flag,$msg,$data,$db;
      $has = $db->select('member_worktime',['id'],[
        'AND'=>[
            'staffid' => $_COOKIE['staffID'],
            'workday' => date('Y-m-d')
          ]
        ]);
      if(count($has)<2){
        $db->insert('member_worktime',[
            'staffid' => $_COOKIE['staffID'],
            'workday' => date('Y-m-d'),
            'worktime' => date('Y-m-d H:i:s'),
            'ip' => getip()
          ]);
        $json = array('flag' => 200,'msg' => '打卡成功', 'data' => []);
        return $response->withJson($json);
      }else{
        $db->update('member_worktime',[
            'worktime' => date('Y-m-d H:i:s'),
            'ip' => getip()
          ],[
            'id'=>$has[1]['id']
          ]);
        $json = array('flag' => 200,'msg' => '您已经打过两次卡了，建议不要重复打卡。', 'data' => []);
        return $response->withJson($json);
      }
      //
    }

    public function worktimelog($request, $response, $args){
      global $flag,$msg,$data,$db;
      $path = $this->app->router->pathFor('executiveIndex');
      $sc = $_COOKIE['subcomid'];
      if(isset($args['year'])){
        $year = $args['year'];
      }else{
        $year = date('Y');
      }

      if(isset($args['month'])){
        $month = $args['month'];
      }else{
        $month = date('m');
      }

      if(isset($args['day'])){
        $day = $args['day'];
      }else{
        $day = date('d');
      }

      $u = $db->select('member',[
          'member.id(id)',
          'member.name(name)'
        ],[
          'AND'=>[
            'status' => 1,
            'subcompany' => $sc
          ]
        ]);
      $list = [];
      $i=0;
      foreach ($u as $ul) {
        $list[$i]['id'] = $ul['id'];
        $list[$i]['name'] = $ul['name'];
        $wt = $db->select('member_worktime','*',[
            'AND'=>[
              'member_worktime.workday'=>"$year-$month-$day",
              'staffid'=>$ul['id']
            ]
            
          ]);
        $list[$i]['worktime'] = $wt;
        $list[$i]['workday'] = "$year-$month-$day";
        $i++;
      }
      // var_dump($list);
      // exit;
      // $list = $db->select('member',[
      //   '[><]member_worktime'=>['id'=>'staffid']
      //   ],[
      //     'member.name(name)',
      //     'member_worktime.workday(workday)',
      //     'member_worktime.worktime(worktime)',
      //     'member_worktime.ip(ip)'
      //   ]);

      $as = [
        'settings'=>$this->app->get('settings'),
        'path' => $path,
        'list' => $list,
        'thisyear' => $year,
        'thismonth' => $month,
        'thisday' => $day
      ];
      return $this->app->renderer->render($response, './executive_kq.php', $as);
    }

    //外勤事务
    public function minelog($request, $response, $args){
       global $flag,$msg,$data,$db;
      $path = $this->app->router->pathFor('executiveIndex');
      $sc = $_COOKIE['subcomid'];
      // var_dump($args);
      // exit;
       if(isset($args['year'])){
        $year = $args['year'];
      }else{
        $year = date('Y');
      }

      if(isset($args['month'])){
        $month = $args['month'];
      }else{
        $month = date('m');
      }

      if(isset($args['day'])){
        $day = $args['day'];
      }else{
        $day = date('d');
      }
      $u = $db->select('member',[
          'member.id(id)',
          'member.name(name)'
        ],[
          'AND'=>[
            'status' => 1,
            'subcompany' => $sc
          ]
        ]);
      // var_dump($u);
      // exit;
    
      // var_dump($wt);
      // exit;
    //查询外勤表信息
     $c=$db->select('member_mine',[
         'member_mine.id(id)',
          'member_mine.name(name)',
          'member_mine.shiwu(shiwu)',
          'member_mine.ortime(ortime)',
          'member_mine.resd(resd)',
          'member_mine.worktime(worktime)',          
      ]);
      // var_dump($c);
      // exit;
       $list = [];
       $i=0;
      foreach ($c as $ul) {
        $list[$i]['id'] = $ul['id'];
        $list[$i]['name'] = $ul['name'];
        $list[$i]['shiwu']=$ul['shiwu'];
        $list[$i]['ortime']=$ul['ortime'];
        $list[$i]['resd']=$ul['resd'];

        $wt = $db->select('member_mine','*',[
                 'member_mine.id(id)',
                  'member_mine.name(name)',
                  'member_mine.shiwu(shiwu)',
                  'member_mine.ortime(ortime)',
            'AND'=>[
              'member_mine.worktime'=>"$year-$month-$day",

            ]            
          ]);
        $list[$i]['worktime'] = "$year-$month-$day";
        $i++;
      }
      $as = [
        'settings'=>$this->app->get('settings'),
        'path' => $path,      
        'thisyear' => $year,
        'thismonth' => $month,
        'thisday' => $day,
        'list' => $list,
        'c'=>$c
        
      ];
      return $this->app->renderer->render($response, './executive_wq.php', $as);
       
    }

    //查看外勤具体信息
    public function notdetail($request, $response, $args){      
       global $flag,$msg,$data,$db;
      $id=$args;
      $path = $this->app->router->pathFor('executiveIndex');
      $sc = $_COOKIE['subcomid'];
      if(isset($args['year'])){
        $year = $args['year'];
      }else{
        $year = date('Y');
      }

      if(isset($args['month'])){
        $month = $args['month'];
      }else{
        $month = date('m');
      }

      if(isset($args['day'])){
        $day = $args['day'];
      }else{
        $day = date('d');
      }

      $u = $db->select('member',[
          'member.id(id)',
          'member.name(name)'
        ],[
          'AND'=>[
            'status' => 1,
            'subcompany' => $sc
          ]
        ]);
    //查询外勤表信息
     $mode=$db->get('member_mine','*',[
        'id'=>$id,
      ]);
      $as = [
        'settings'=>$this->app->get('settings'),
        'path' => $path,      
        'thisyear' => $year,
        'thismonth' => $month,
        'thisday' => $day,
        'mode'=>$mode
        
      ];     
      return $this->app->renderer->render($response, './executive_detail.php', $as);    
    }

    //出外勤信息添加
    public function notadd($request,$response,$args){
       global $flag,$msg,$data,$db;
        $id=$args;
      $path = $this->app->router->pathFor('executiveIndex');
      $sc = $_COOKIE['subcomid'];
      $mo=$_COOKIE['mobile'];
      //查询用户信息
      $mode=$db->get('member','*',[
        'mobile'=>$mo,
      ]);
      // 查询部门信息
             $b=$db->get('member_department',[
                'member_department.departmentname(departmentname)',
              ],[
                'id'=>$mode['department'],
              ]);
      if(isset($args['year'])){
        $year = $args['year'];
      }else{
        $year = date('Y');
      }

      if(isset($args['month'])){
        $month = $args['month'];
      }else{
        $month = date('m');
      }

      if(isset($args['day'])){
        $day = $args['day'];
      }else{
        $day = date('d');
      }

      $u = $db->select('member',[
          'member.id(id)',
          'member.name(name)'
        ],[
          'AND'=>[
            'status' => 1,
            'subcompany' => $sc
          ]
        ]);

      $as = [
        'settings'=>$this->app->get('settings'),
        'path' => $path,      
        'thisyear' => $year,
        'thismonth' => $month,
        'thisday' => $day,
        'mode'=>$mode,
        'b'=>$b,               
      ];    
      return $this->app->renderer->render($response, './executive_add.php',$as);     
    }

    //外勤信息添加
    public function notinsert($request,$response,$args){
        global $flag,$msg,$data,$db;
           $sc = $_COOKIE['subcomid'];
           $mo=$_COOKIE['mobile'];
          $time=$_POST['YYYY'].'-'.$_POST['MM'].'-'.$_POST['DD'].' '.$_POST['HH'].'时'.$_POST['II'].'分';
          $ortime=$_POST['YY'].'-'.$_POST['MO'].'-'.$_POST['DE'].' '.$_POST['H'].'时'.$_POST['I'].'分';
           $s=date('Y-m-d H:i:s');
           $t=date('Y-m-d');  
            $a=$db->insert('member_mine',[
              'name' => $_POST['name'],
              'mobile' => $_POST['mobile'],
              'shiwu' => $_POST['shiwu'],
              'address' => $_POST['address'],
              'time' => $time,
              'departmentname'=>$_POST['departmentname'],
              'ortime' => $ortime,
              'shuo' => $_POST['shuo'],              
              'resd' => $s,
              'worktime' => $t,
              ]);
             $path = $this->app->router->pathFor('executiveIndex');
     

      if(isset($args['year'])){
        $year = $args['year'];
      }else{
        $year = date('Y');
      }

      if(isset($args['month'])){
        $month = $args['month'];
      }else{
        $month = date('m');
      }

      if(isset($args['day'])){
        $day = $args['day'];
      }else{
        $day = date('d');
      }

      $u = $db->select('member',[
          'member.id(id)',
          'member.name(name)'
        ],[
          'AND'=>[
            'status' => 1,
            'subcompany' => $sc
          ]
        ]);
    //查询外勤表信息
     $c=$db->select('member_mine',[
         'member_mine.id(id)',
          'member_mine.name(name)',
          'member_mine.shiwu(shiwu)',
          'member_mine.ortime(ortime)',
           'member_mine.resd(resd)',           
      ]);
      $as = [
        'settings'=>$this->app->get('settings'),
        'path' => $path,      
        'thisyear' => $year,
        'thismonth' => $month,
        'thisday' => $day,
        'c'=>$c
        
      ];          
      return $this->app->renderer->render($response, './executive_wq.php', $as);           
    }

    //修改外勤信息、、、、、、、、、、、
    public function notupdate($request,$response,$args){       
       global $flag,$msg,$data,$db;
       // var_dump($_POST);
       //修改信息
       $s=date('Y-m-d H:i:s'); 
       $id=$_POST['id'];
       $a=$db->update('member_mine',[
              'name' => $_POST['name'],
              'mobile' => $_POST['mobile'],
              'shiwu' => $_POST['shiwu'],
              'address' => $_POST['address'],
              'time' => $_POST['time'],
              'ortime' => $_POST['ortime'],
              'shuo' => $_POST['shuo'],              
              'resd' => $s,
        ],[
          'id'=>$id,
        ]);

           $path = $this->app->router->pathFor('executiveIndex');
      $sc = $_COOKIE['subcomid'];

      if(isset($args['year'])){
        $year = $args['year'];
      }else{
        $year = date('Y');
      }

      if(isset($args['month'])){
        $month = $args['month'];
      }else{
        $month = date('m');
      }

      if(isset($args['day'])){
        $day = $args['day'];
      }else{
        $day = date('d');
      }

      $u = $db->select('member',[
          'member.id(id)',
          'member.name(name)'
        ],[
          'AND'=>[
            'status' => 1,
            'subcompany' => $sc
          ]
        ]);
    //查询外勤表信息
     $c=$db->select('member_mine',[
         'member_mine.id(id)',
          'member_mine.name(name)',
          'member_mine.shiwu(shiwu)',
          'member_mine.ortime(ortime)',
           'member_mine.resd(resd)',                                  
      ]);
      $as = [
        'settings'=>$this->app->get('settings'),
        'path' => $path,      
        'thisyear' => $year,
        'thismonth' => $month,
        'thisday' => $day,
        'c'=>$c        
      ];          
      return $this->app->renderer->render($response, './executive_wq.php', $as);        
    }

    //加载查询节日福利列表//////////////////////////////////
    public function notselect($request,$response,$args){
         global $flag,$msg,$data,$db;
          $path = $this->app->router->pathFor('executiveIndex');
          $sc = $_COOKIE['subcomid'];
          //查询节日列表信息
          $j=$db->select('member_welfare',[
              'member_welfare.id(id)',
              'member_welfare.jname(jname)',
              'member_welfare.jtime(jtime)',
              'member_welfare.wftime(wftime)',
            ]);
          if(isset($args['year'])){
            $year = $args['year'];
          }else{
            $year = date('Y');
          }

          if(isset($args['month'])){
            $month = $args['month'];
          }else{
            $month = date('m');
          }

          if(isset($args['day'])){
            $day = $args['day'];
          }else{
            $day = date('d');
          }

          $u = $db->select('member',[
              'member.id(id)',
              'member.name(name)'
            ],[
              'AND'=>[
                'status' => 1,
                'subcompany' => $sc
              ]
            ]);
          $list = [];
          $i=0;
          foreach ($u as $ul) {
            $list[$i]['id'] = $ul['id'];
            $list[$i]['name'] = $ul['name'];
            $wt = $db->select('member_worktime','*',[
                'AND'=>[
                  'member_worktime.workday'=>"$year-$month-$day",
                  'staffid'=>$ul['id']
                ]
                
              ]);
            $list[$i]['worktime'] = $wt;
            $list[$i]['workday'] = "$year-$month-$day";
            $i++;
          }    

          $as = [
            'settings'=>$this->app->get('settings'),
            'path' => $path,
            'list' => $list,
            'thisyear' => $year,
            'thismonth' => $month,
            'thisday' => $day,
            'j'=>$j,
          ];
      return $this->app->renderer->render($response, './executive_jie.php', $as);
    }
    //添加节日页面。。。。。。。。。。。。。。。。。。。。
    public function notjieadd($request,$response,$args){
      global $flag,$msg,$data,$db;
        $id=$args;
      $path = $this->app->router->pathFor('executiveIndex');
      $sc = $_COOKIE['subcomid'];
      $mo=$_COOKIE['mobile'];
      //查询用户信息
      $mode=$db->get('member','*',[
        'mobile'=>$mo,
      ]);
      // 查询部门信息
             $b=$db->get('member_department',[
                'member_department.departmentname(departmentname)',
              ],[
                'id'=>$mode['department'],
              ]);
      if(isset($args['year'])){
        $year = $args['year'];
      }else{
        $year = date('Y');
      }

      if(isset($args['month'])){
        $month = $args['month'];
      }else{
        $month = date('m');
      }

      if(isset($args['day'])){
        $day = $args['day'];
      }else{
        $day = date('d');
      }

      $u = $db->select('member',[
          'member.id(id)',
          'member.name(name)'
        ],[
          'AND'=>[
            'status' => 1,
            'subcompany' => $sc
          ]
        ]);

      $as = [
        'settings'=>$this->app->get('settings'),
        'path' => $path,      
        'thisyear' => $year,
        'thismonth' => $month,
        'thisday' => $day,
        'mode'=>$mode,
        'b'=>$b,               
      ];    
      return $this->app->renderer->render($response, './executive_jieadd.php',$as);   
    }
    //节日信息写入数据库
    public function notjieinsert($request,$response,$args){
           global $flag,$msg,$data,$db;
          $path = $this->app->router->pathFor('executiveIndex');
          $sc = $_COOKIE['subcomid'];
          $mo=$_COOKIE['mobile'];
          $time=$_POST['YYYY'].'-'.$_POST['MM'].'-'.$_POST['DD'];
          $s=date('Y-m-d H:i:s');
          // var_dump($_POST);
          // exit;
          //写入数据库
           $i=$db->insert('member_welfare',[
                'jname' => $_POST['jname'],
                'jtime'=>$time,
                'jcontent'=>$_POST['jcontent'],
                'wftime'=>$s,
            ]);
          //查询节日列表信息
          $j=$db->select('member_welfare',[
              'member_welfare.id(id)',
              'member_welfare.jname(jname)',
              'member_welfare.jtime(jtime)',
              'member_welfare.wftime(wftime)',
            ]);     
          $path = $this->app->router->pathFor('executiveIndex');
          if(isset($args['year'])){
            $year = $args['year'];
          }else{
            $year = date('Y');
          }

          if(isset($args['month'])){
            $month = $args['month'];
          }else{
            $month = date('m');
          }

          if(isset($args['day'])){
            $day = $args['day'];
          }else{
            $day = date('d');
          }

          $u = $db->select('member',[
              'member.id(id)',
              'member.name(name)'
            ],[
              'AND'=>[
                'status' => 1,
                'subcompany' => $sc
              ]
            ]);
          $list = [];
          $i=0;
          foreach ($u as $ul) {
            $list[$i]['id'] = $ul['id'];
            $list[$i]['name'] = $ul['name'];
            $wt = $db->select('member_worktime','*',[
                'AND'=>[
                  'member_worktime.workday'=>"$year-$month-$day",
                  'staffid'=>$ul['id']
                ]
                
              ]);
            $list[$i]['worktime'] = $wt;
            $list[$i]['workday'] = "$year-$month-$day";
            $i++;
          }    
          $as = [
            'settings'=>$this->app->get('settings'),
            'path' => $path,
            'list' => $list,
            'thisyear' => $year,
            'thismonth' => $month,
            'thisday' => $day,
            'j'=>$j,
          ];
      return $this->app->renderer->render($response, './executive_jie.php', $as);

    }
    //查看节日详细信息
    public function notjedit($request,$response,$args){
      global $flag,$msg,$data,$db;
      $id=$args;
      $path = $this->app->router->pathFor('executiveIndex');
      $sc = $_COOKIE['subcomid'];
      $mo=$_COOKIE['mobile'];
      //查询详细信息
      $jie=$db->get('member_welfare','*',[
          'id'=>$id,
        ]);
      if(isset($args['year'])){
        $year = $args['year'];
      }else{
        $year = date('Y');
      }

      if(isset($args['month'])){
        $month = $args['month'];
      }else{
        $month = date('m');
      }

      if(isset($args['day'])){
        $day = $args['day'];
      }else{
        $day = date('d');
      }

      $u = $db->select('member',[
          'member.id(id)',
          'member.name(name)'
        ],[
          'AND'=>[
            'status' => 1,
            'subcompany' => $sc
          ]
        ]);

      $as = [
        'settings'=>$this->app->get('settings'),
        'path' => $path,      
        'thisyear' => $year,
        'thismonth' => $month,
        'thisday' => $day, 
        'jie'=>$jie,              
      ];    
      return $this->app->renderer->render($response, './executive_jedit.php',$as);   
    }
    //修改节日详细信息
    public function notjupdate($request,$response,$args){
           global $flag,$msg,$data,$db;
          $path = $this->app->router->pathFor('executiveIndex');
          $sc = $_COOKIE['subcomid'];
          $mo=$_COOKIE['mobile'];
          $time=$_POST['YYYY'].'-'.$_POST['MM'].'-'.$_POST['DD'];
           $s=date('Y-m-d H:i:s');
          $id=$_POST['id'];
          // var_dump($_POST);
          // exit;
          // 将修改的数据写入数据库
          $update=$db->update('member_welfare',[
              'jname' => $_POST['jname'],
              'jtime' => $time,
              'jcontent' => $_POST['jcontent'],
              'wftime' => $s,                                        
            ],[
            'id'=>$id,
            ]);
         

          //查询节日列表信息
          $j=$db->select('member_welfare',[
              'member_welfare.id(id)',
              'member_welfare.jname(jname)',
              'member_welfare.jtime(jtime)',
              'member_welfare.wftime(wftime)',
            ]);     
          $path = $this->app->router->pathFor('executiveIndex');
          if(isset($args['year'])){
            $year = $args['year'];
          }else{
            $year = date('Y');
          }

          if(isset($args['month'])){
            $month = $args['month'];
          }else{
            $month = date('m');
          }

          if(isset($args['day'])){
            $day = $args['day'];
          }else{
            $day = date('d');
          }

          $u = $db->select('member',[
              'member.id(id)',
              'member.name(name)'
            ],[
              'AND'=>[
                'status' => 1,
                'subcompany' => $sc
              ]
            ]);
          $list = [];
          $i=0;
          foreach ($u as $ul) {
            $list[$i]['id'] = $ul['id'];
            $list[$i]['name'] = $ul['name'];
            $wt = $db->select('member_worktime','*',[
                'AND'=>[
                  'member_worktime.workday'=>"$year-$month-$day",
                  'staffid'=>$ul['id']
                ]
                
              ]);
            $list[$i]['worktime'] = $wt;
            $list[$i]['workday'] = "$year-$month-$day";
            $i++;
          }    
          $as = [
            'settings'=>$this->app->get('settings'),
            'path' => $path,
            'list' => $list,
            'thisyear' => $year,
            'thismonth' => $month,
            'thisday' => $day,
            'j'=>$j,
            
          ];
      return $this->app->renderer->render($response, './executive_jie.php', $as);


    }

}
